package com.kdd.campus.sh.service.impl;

import com.kdd.campus.sh.dao.ShCartMapper;
import com.kdd.campus.sh.dao.ShProductMapper;
import com.kdd.campus.sh.dao.UserMapper;
import com.kdd.campus.sh.dto.CartDto;
import com.kdd.campus.sh.po.ShCart;
import com.kdd.campus.sh.po.ShProduct;
import com.kdd.campus.sh.po.User;
import com.kdd.campus.sh.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Auther: fcp
 * @Date: 2020/4/15 15:51
 * @Description:
 */
@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private ShCartMapper cartMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ShProductMapper productMapper;

    @Override
    public void add(ShCart cart) {
        Example example = new Example(ShCart.class);
        example.createCriteria()
                .andEqualTo("userId",cart.getUserId())
                .andEqualTo("productId",cart.getProductId())
                .andEqualTo("status",1);
        ShCart result = cartMapper.selectOneByExample(example);
        if (result != null){
            // 如果之前购物车已经添加过,则数量+number即可
            result.setNumber(result.getNumber() + cart.getNumber());
            cartMapper.updateByPrimaryKeySelective(result);
        }else {
            // 第一次添加则执行插入操作
            cart.setStatus(1);
            cart.setCreateTime(new Date());
            cart.setNumber(cart.getNumber());
            cartMapper.insert(cart);
        }
    }

    @Override
    public void delete(Long userId, Long id) {
        ShCart cart = ShCart.builder().status(0).build();
        Example example = new Example(ShCart.class);
        example.createCriteria()
                .andEqualTo("id",id)
                .andEqualTo("userId", userId);

        cartMapper.updateByExampleSelective(cart,example);
    }

    @Override
    public void empty(Long userId) {
        Example example = new Example(ShCart.class);
        example.createCriteria().andEqualTo("userId",userId);

        ShCart cart = ShCart.builder().status(0).build();
        cartMapper.updateByExampleSelective(cart, example);
    }

    @Override
    public void update(Long userId, Long cartId, Integer productNum) {
        ShCart cart = ShCart.builder().number(productNum).build();

        Example example = new Example(ShCart.class);
        example.createCriteria()
                .andEqualTo("id",cartId)
                .andEqualTo("userId",userId);

        cartMapper.updateByExampleSelective(cart, example);
    }

    @Override
    public List<CartDto> findByUserId(Long userId) {
        Example example = new Example(ShCart.class);
        example.createCriteria().andEqualTo("userId", userId).andEqualTo("status", 1);
        List<ShCart> cartList = cartMapper.selectByExample(example);
        List<CartDto> cartDtoList = new ArrayList<>();
        for (ShCart cart : cartList){
            User user = userMapper.selectByPrimaryKey(cart.getUserId());
            ShProduct product = productMapper.selectByPrimaryKey(cart.getProductId());
            CartDto cartDto = new CartDto();
            cartDto.setProductId(product.getId());
            cartDto.setId(cart.getId());
            cartDto.setProductNum(cart.getNumber());
            cartDto.setCreated(cart.getCreateTime());
            cartDto.setImage(product.getCoverUrl());
            cartDto.setIsNew(product.getIsNew());
            cartDto.setName(product.getName());
            cartDto.setPrice(product.getPrice());
            cartDto.setUserId(user.getId());
            cartDto.setUserImg(user.getHeadPortrait());
            cartDto.setUserName(user.getUsername());
            cartDtoList.add(cartDto);
        }

        return cartDtoList;
    }


}
